#ifndef Q3_HPP

#define Q3_HPP
#include "iostream"
#include "vector"
#include <map>
#include <stdlib.h>
#include <fstream>
#include <algorithm>
using namespace std;

class PhoneBook
{
private:
    map<string, string> phonemap;
    pair<string, string> pTransLineToPair(const string &line)
    {
        int i = line.find(" ");
        if (i == -1)
        {
            throw exception();
        }
        cout << i << endl;
        string stringnumber = line.substr(0, i);
        string name = line.substr(i + 1, line.size());
        return make_pair(name, stringnumber);
    }
    class mapValueFinder
    {
    private:
        string phoneNumber;

    public:
        mapValueFinder(const string &_number) : phoneNumber(_number) {}
        bool operator()(const map<string, string>::value_type &pair)
        {
            return !bool(pair.second.compare(phoneNumber));
        }
        ~mapValueFinder();
    };
    /* data */
public:
    PhoneBook();
    ~PhoneBook();
    bool isNotFound(map<string, string>::iterator ite) { return ite == phonemap.end(); }
    map<string, string>::iterator iFind(string _name, int type);
    void vExportPhoneBook(const string &filename);
    void vImportPhoneBook(const string &path);
    void vAddContact(string name, string number);
    void vAddContact(const pair<string, string> p);
};
PhoneBook::PhoneBook()
{
}

PhoneBook::~PhoneBook()
{
}
void PhoneBook::vAddContact(string name, string number)
{
    vAddContact(make_pair(name, number));
}
void PhoneBook::vAddContact(const pair<string, string> p)
{
    phonemap.insert(p);
}

void PhoneBook::vImportPhoneBook(const string &path)
{
    char buffer[200];
    ifstream infile(path, ios::in);
    while (infile.good())
    {
        infile.getline(buffer, 400);
        string str(buffer);
        if (str != "")
            vAddContact(pTransLineToPair(str));
    }
    infile.close();
}
void PhoneBook::vExportPhoneBook(const string &filename)
{
    cout << "开始导出通信录" << endl;
    ofstream outfile(filename, ios::out | ios::trunc);
    for (auto ite = phonemap.begin(); ite != phonemap.end(); ++ite)
    {
        string number = (*ite).second;
        outfile << (*ite).first << " " << number << endl;
        cout << "*";
    }
    cout << "#" << endl
         << "输出完成" << endl;
    outfile.close();
}
map<string, string>::iterator PhoneBook::iFind(string _name, int type)
{
    if (type == 0)
    {
        return phonemap.find(_name);
    }
    else
    {
        return find_if(phonemap.begin(), phonemap.end(), [_name](const auto &item)
                       { return !bool(item.second.compare(_name)); });
    }
}

void q3()
{
    PhoneBook qb;
    qb.vAddContact("ddv", "19921314545");
    qb.vAddContact("yjx", "17889891973");
    qb.vExportPhoneBook("qb.txt");
    qb.vImportPhoneBook("./qb.txt");
    map<string, string>::iterator ite = qb.iFind("ddv1", 1);
    if (qb.isNotFound(ite))
        cout << "can not find that contact" << endl;
    else
        cout << (*ite).first << endl;
}
#endif